package com.yztstudy.Controller;

import com.yztstudy.Entity.FoodType;
import com.yztstudy.Service.FoodTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * FoodType的表示层
 */

@Controller
@Scope("prototype") //spring注解的bean可以变成多例
public class FoodTypeController {

    @Autowired
    private FoodTypeService foodTypeService;

    /**
     * 查询所有
     */
    @GetMapping("/foodTypeQueryList")
    public String queryList(ModelMap map)
    {
        List<FoodType> foodTypes = foodTypeService.getFoodTypes("");
        map.addAttribute("foodTypes", foodTypes);
        return "foodTypeList";
    }

    /**
     * 条件查询
     */
    @PostMapping("/foodTypeQueryList")
    public String queryByCondition(String keyword, ModelMap map)
    {
        List<FoodType> foodTypes = foodTypeService.getFoodTypes(keyword);
        //回显数据
        map.addAttribute("foodTypes", foodTypes);
        map.addAttribute("keyword", keyword);
        return "foodTypeList";
    }

    /**
     * 新增菜系
     */
    @PostMapping("/foodTypeAdd")
    public void addFoodType(FoodType foodType, HttpServletResponse response) throws IOException {

        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = foodTypeService.addFoodType(foodType);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('菜系新增失败!'); location.href = '/page_saveFoodType';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('菜系新增成功!'); location.href = '/foodTypeQueryList';</script>");
            }else {
                response.getWriter().write("<script>alert('菜系新增失败!'); location.href = '/page_saveFoodType';</script>");
            }
        }
    }

    /**
     * 跳转页面
     */
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, ModelMap map)
    {
        map.addAttribute("id", id);
        map.addAttribute("foodTypeName", foodTypeService.getFoodTypeById(id).getName());
        return "updateFoodType";
    }

    /**
     * 更新菜系
     */
    @PostMapping("/foodTypeUpdate")
    public void updateFoodType(FoodType foodType, HttpServletResponse response) throws IOException
    {
        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = foodTypeService.updateFoodType(foodType);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('菜系更新失败!'); location.href = '/page_updateFoodType';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('菜系更新成功!'); location.href = '/foodTypeQueryList';</script>");
            }else {
                response.getWriter().write("<script>alert('菜系更新失败!'); location.href = '/page_updateFoodType';</script>");
            }
        }
    }

    /**
     * 删除菜系
     */
    @RequestMapping("/foodTypeDelete/{id}")
    public void deleteFoodType(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException
    {
        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = foodTypeService.deleteFoodType(id);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('删除菜系失败!'); location.href = '/foodTypeQueryList';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('删除菜系成功!'); location.href = '/foodTypeQueryList';</script>");
            }else {
                response.getWriter().write("<script>alert('删除菜系失败!'); location.href = '/foodTypeQueryList';</script>");
            }
        }
    }

    /**
     * 激活菜系
     */
    @RequestMapping("/foodTypeActive/{id}")
    public void activeFoodType(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException
    {
        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = foodTypeService.activeFoodType(id);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('激活菜系失败!'); location.href = '/foodTypeQueryList';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('激活菜系成功!'); location.href = '/foodTypeQueryList';</script>");
            }else {
                response.getWriter().write("<script>alert('激活菜系失败!'); location.href = '/foodTypeQueryList';</script>");
            }
        }
    }
}
